package com.scalar.chiptrack.finance.costCode.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.costCode.forms.CostCodeMasterForm;
import com.scalar.chiptrack.finance.costCode.dao.CostCodeMasterDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * User: KP
 * Date: Aug 03, 2007
 * Time: 2:46:50 PM
 */
public class CostCodeMasterLoadCommand extends ActionCommand
{

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        HttpSession session = request.getSession();
        User user           = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        
        HashMap errors      = ( HashMap ) request.getAttribute( SessionKeys.ERROR_MESSAGES );
        HashMap info        = ( HashMap ) request.getAttribute( SessionKeys.INFO_MESSAGES );

        if ( user == null )
        {
            session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  "costCodeMaster.do" );
            return ( mapping.findForward ( "login" ) );
        }

        CostCodeMasterForm costCodeForm = ( CostCodeMasterForm ) form;

        try
        {
            costCodeForm.setWaferCodeCodeList   ( CostCodeMasterDAO.loadWaferPartNumber( null ) );
            costCodeForm.setPackageCodeCodeList ( CostCodeMasterDAO.loadPackageCostCodes( null ) );
            costCodeForm.setPinCostCodeList     ( CostCodeMasterDAO.loadPinCostCodes( null ) );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        return mapping.findForward( "success" );
    }
}
